package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.AnnotationUtils;
import com.ruoyi.enterwarehouse.domain.WEnterWarehouseInfo;
import com.ruoyi.enterwarehouse.service.IWEnterWarehouseInfoService;
import com.ruoyi.exitwarehouse.domain.WExitWarehouseInfo;
import com.ruoyi.exitwarehouse.service.IWExitWarehouseInfoService;
import com.ruoyi.goods.domain.TGoodsInfo;
import com.ruoyi.goods.service.ITGoodsInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Set;
import static com.ruoyi.common.utils.Threads.sleep;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    @Autowired
    private IWEnterWarehouseInfoService wEnterWarehouseInfoService;

    @Autowired
    private ITGoodsInfoService tGoodsInfoService;

    @Autowired
    private IWExitWarehouseInfoService wExitWarehouseInfoService;

    private static Logger logger = LoggerFactory.getLogger(RyTask.class);

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
        TGoodsInfo goodsInfo = new TGoodsInfo();
        goodsInfo.setAgreementNumber("1234567890ABCDEFG");
        System.out.println("goodsInfo.setAgreementNumber(\"1234567890ABCDEFG\");");
        System.out.println("goodsInfo.getAgreementNumber() = " + goodsInfo.getAgreementNumber());
        // 创建Validator实例
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();

        // 校验对象
        Set<ConstraintViolation<TGoodsInfo>> violations = validator.validate(goodsInfo);

        // 输出校验结果
        if (!violations.isEmpty()) {
            for (ConstraintViolation<TGoodsInfo> violation : violations) {
                System.out.println(violation.getMessage());
                System.out.println(violation.toString());
                System.out.println(violation.getRootBean().getAgreementNumber());
                System.out.println(violation.getConstraintDescriptor().getAnnotation().toString());
                Annotation annotation = violation.getConstraintDescriptor().getAnnotation();
                System.out.println(annotation.annotationType().toString());
                System.out.println(annotation.annotationType());
                Integer max = 0;
                try {
                    max = (Integer) AnnotationUtils.getAnnotationValue(annotation, "max");
                    System.out.println(max);
                    System.out.println(violation.getPropertyPath());

                    //获取当前验证的对象实例
                    Object validatedObject = violation.getRootBean();
                    // 获取被验证的字段名称
                    String propertyName = violation.getPropertyPath().toString();
                    // 使用反射获取字段对象
                    Field field = validatedObject.getClass().getDeclaredField(propertyName);
                    // 设置字段为可访问
                    field.setAccessible(true);
                    // 获取被验证的字段值
                    String value = (String)field.get(violation.getRootBean());
                    // 截断被验证的字符串
                    String truncatedValue = value.substring(0, max);
                    // 修改被验证的字段值
                    field.set(validatedObject, truncatedValue);

                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            System.out.println("校验通过");
        }
        System.out.println("goodsInfo.getAgreementNumber() = " + goodsInfo.getAgreementNumber());
        // ConstraintViolationImpl{interpolatedMessage='合约号长度16个字符之间', propertyPath=agreementNumber, rootBeanClass=class com.ruoyi.goods.domain.TGoodsInfo, messageTemplate='合约号长度16个字符之间'}
    }

    public void calculateEnterWeightPiece()
    {
        logger.debug("计算入库单，重量，件数，开始 ...");
        WEnterWarehouseInfo wEnterWarehouseInfo = new WEnterWarehouseInfo();
        List<WEnterWarehouseInfo> list = wEnterWarehouseInfoService.selectWEnterWarehouseInfoList(wEnterWarehouseInfo);
        for(WEnterWarehouseInfo enterWarehouseInfo : list)
        {
            wEnterWarehouseInfoService.updateWEnterTotalWeightPieceByNumber(enterWarehouseInfo.getEnterWarehouseNumber());
        }
        logger.debug("计算入库单，重量，件数，结束 ...");
    }

    public void calculateWarehouseLocate()
    {
        logger.debug("计算货物，仓位，开始 ...");
        TGoodsInfo tGoodsInfo = new TGoodsInfo();
        List<TGoodsInfo> list = tGoodsInfoService.selectTGoodsInfoList(tGoodsInfo);
        for(TGoodsInfo goods : list)
        {
            goods.setWarehouseTotalLocate(goods.getLogisticsParkName() + goods.getWarehouseLocateName() + goods.getRegionLocateName() + goods.getGoodsLocateName());
            tGoodsInfoService.updateTGoodsInfo(goods);
        }
        logger.debug("计算货物，仓位，结束 ...");
    }

    public void synchroniseTransportMark()
    {
        logger.debug("同步，运输标记，从出库单据到货物信息，开始 ...");
        WExitWarehouseInfo wExitWarehouseInfo = new WExitWarehouseInfo();
        List<WExitWarehouseInfo> list = wExitWarehouseInfoService.selectWExitWarehouseInfoList(wExitWarehouseInfo);
        for(WExitWarehouseInfo exitWarehouseInfo : list)
        {
            wExitWarehouseInfo.setExitWarehouseCustomer(exitWarehouseInfo.getExitWarehouseCustomer());
            wExitWarehouseInfo.setExitWarehouseNumber(exitWarehouseInfo.getExitWarehouseNumber());
            wExitWarehouseInfo.setExitTransportMark(exitWarehouseInfo.getExitTransportMark());
            wExitWarehouseInfoService.updateWExitWarehouseInfo(wExitWarehouseInfo);
        }
        logger.debug("同步，运输标记，从出库单据到货物信息，结束 ...");
    }

}
